Chapter 7 Visualizing Spatial Data
7.1 Lesson Goals
- Explore several mapping libraries in R
- Construct a couple example visualizations with spatial data in R
R is fantastic for making publication quality static maps, and for generating repetitive graphics through scripts; we’ve already seen how to make simple maps using base plotting,ggplot, and tmap. There are also a number of packages in R that link R code to plotting libraries developed in Javascript (or other languages) for interactive plotting and web integration.
It can be hard to decide which mapping packages to learn and use - some nice advice from Martin Tennekes who created tmap:
- If you know some ggplot, don’t care about interactive maps, and don’t want to spend a lot of time learning new packages, use ggplot
- If you want interactive maps as flexible as possible, use leaflet
- If you want to simply explore spatial objects ineractively as easily as possible, use mapview
- Otherwise, use tmap!
7.2 tmap
Load tidycensus - you’ll need to set your Census API key. A key can be obtained from here.
library(sf,quietly = T)
library(tidycensus)
# census_api_key("YOUR API KEY GOES HERE")Here we’ll start with a simple choropleth map of median household income in Benton county in Oregon.
library(tidycensus)
library(tmap)
options(tigris_use_cache = FALSE)
benton_tracts <- get_acs(state = 'OR', county = 'Benton', geography = "tract",
variables = "B19013_001", geometry = TRUE)
# Rename a column
benton_tracts <- benton_tracts %>%
dplyr::rename(MedianIncome=estimate)
tm_shape(benton_tracts) + tm_polygons(col="MedianIncome")
That’s a pretty basic map - we can adjust a number of settings: - breaks: we can set different breaks for our map - n: we can control the number of bins - palette: we can change the color palette
These are just a few - let’s play with those to start.
breaks = c(0, 40, 80, 120, 140) * 1000
t1 <- tm_shape(benton_tracts) + tm_polygons(col="MedianIncome", breaks=breaks)
t2 <- tm_shape(benton_tracts) + tm_polygons(col = "MedianIncome", n = 10)
t3 <- tm_shape(benton_tracts) + tm_polygons(col = "MedianIncome", palette = "BuGn")
tmap_arrange(t1, t2, t3, nrow = 1)
tmap also has view mode which can be handy
tmap_mode("view")
tm_shape(benton_tracts) + tm_polygons(col="MedianIncome")We can make multi-layered maps (shout out to Amalia Handler who used this in tmap demo for our lab group)
# Load data
data(World, metro, rivers, land)
# Set to static mode
tmap_mode("plot")
# Make map that is multilayered
tm_shape(land) +
tm_raster("elevation", palette = terrain.colors(10)) +
tm_shape(World) +
tm_borders("white", lwd = .5) +
tm_text("iso_a3", size = "AREA") +
tm_shape(metro) +
tm_symbols(col = "red", size = "pop2020", scale = .5) +
tm_legend(show = FALSE)
We can make faceted maps with tmap
# Set mode to interactive
tmap_mode("view")
# Plot it out
tm_shape(World) +
tm_polygons(c("pop_est", "HPI","life_exp","well_being")) +
tm_facets(sync = TRUE, ncol = 2)7.3 leaflet
Leaflet is an extremely popular open-source javascript library for interactive web mapping, and the leaflet R package allows R users to create Leaflet maps from R. Leaflet can plot sf or sp objects, or x / y coordinates, and can plot points, lines or polygons. There are a number of base layers you can choose from. It’s worth spending some time exploring the excellent Leaflet for R site.
Here we make the simplest of leaflet maps:
library(leaflet)
m <- leaflet() %>%
addTiles() %>% # Add default OpenStreetMap map tiles
addMarkers(lng=-123.26720, lat=44.5810, popup="Here's my house")
m # Print the map7.4 mapview
Mapview is a package designed for quick and easy interactive visualizations of spatial data - it makes use of leaflet but simplifies mapping functions compared to the leaflet package.
It’s easy to layer features with mapview - you can supply a list of objects to mapview or use + syntax as with ggplot.
library(Rspatialworkshop)
library(mapview)
data(bike_paths)
data(parks)
mapview(bike_paths) + parks